package com.vancouver.knows.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.vancouver.knows.R;

import java.util.List;

/**
 * Created by chenxunlin01 on 2016/4/25.
 */
public class AutoGridLinearlayout extends LinearLayout {


    private AutoGridAdapter autoGridAdapter;

    public AutoGridLinearlayout(Context context) {
        super(context);
        init();
    }

    public AutoGridLinearlayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AutoGridLinearlayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setOrientation(VERTICAL);
    }


    public void setAutoGridAdapter(AutoGridAdapter autoGridAdapter) {
        this.autoGridAdapter = autoGridAdapter;
        renderLayout();
    }

    private void renderLayout() {
        removeAllViews();
        if (autoGridAdapter == null) {
            return;
        }

        if (autoGridAdapter.getCount() == 0) {
            return;
        }

        int row = (autoGridAdapter.getCount() / autoGridAdapter.getColumn()) + (autoGridAdapter.getCount() %
                autoGridAdapter.getColumn() == 0 ? 0 : 1);
        for (int i = 0; i < row; i++) {
            LinearLayout rowLinearLayout = new LinearLayout(getContext());
            rowLinearLayout.setOrientation(HORIZONTAL);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1);
            for (int j = 0; j < autoGridAdapter.getColumn(); j++) {
                int index = i * autoGridAdapter.getColumn() + j;
                if (index >= autoGridAdapter.getCount()) {
                    View empty = new View(getContext());
                    rowLinearLayout.addView(empty, lp);
                } else {
                    View childView = autoGridAdapter.getView(index, rowLinearLayout);
                    rowLinearLayout.addView(childView, lp);
                }

                if (j < autoGridAdapter.getColumn() - 1) {
                    View columnLine = new View(getContext());
                    columnLine.setBackgroundColor(getContext().getResources().getColor(R.color.line_color));
                    LayoutParams rowlp = new LayoutParams(1, LayoutParams.MATCH_PARENT);
                    rowLinearLayout.addView(columnLine, rowlp);
                }
            }

            addView(rowLinearLayout);

            if (i < row - 1) {
                View rowLine = new View(getContext());
                rowLine.setBackgroundColor(getContext().getResources().getColor(R.color.line_color));
                LayoutParams rowlp = new LayoutParams(LayoutParams.MATCH_PARENT, 1);
                addView(rowLine, rowlp);
            }
        }
    }


    public static abstract class AutoGridAdapter<T> {

        protected final int column;
        protected final List<T> mItems;

        public AutoGridAdapter(int column, List<T> data) {
            this.column = column > 0 ? column : 1;
            this.mItems = data;
        }

        public int getCount() {
            return mItems == null ? 0 : mItems.size();
        }


        public int getColumn() {
            return column;
        }

        public abstract View getView(int position, ViewGroup parent);
    }
}
